import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

// ---------------------------------- 删除导入Home组件这行

Vue.use(VueRouter)

const routes = [
  // 登录
  { path: '/login', component: () => import('@/views/Login.vue') },
  // 注册
  { path: '/reg', component: () => import('@/views/Reg.vue') },
  // 后台主页
  {
    path: '/',
    component: () => import('@/views/Home.vue'),
    children: [
      { path: 'home', component: () => import('@/views/Chart.vue') },
      { path: 'user-info', component: () => import('@/views/user/UserInfo.vue') }
    ]
  }
] // ---------------- 清空这个数组

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  /*
    next(): 直接放行
    next(false): 不放行，不跳转
    next('/login'): 不放行，跳转页面
  */
  if (to.path === '/login' || to.path === '/reg') {
    next() // 直接放行
  } else {
    if (store.state.user.token) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
